﻿<?xml version="1.0" encoding="utf-8" ?>

<cls:class
	name="ParallelPortWriter"
	type="http://metalx.org/Ports/Parallel/ParallelPortWriter"
	xmlns="http://metalx.org/Program"
	xmlns:mm="http://metalx.org/i386/Functions/MemoryManager"
	xmlns:dbg="http://metalx.org/i386/Functions/Debug"
	xmlns:bool="http://metalx.org/i386/Functions/Boolean"
	xmlns:byte="http://metalx.org/i386/Functions/Byte"
	xmlns:short="http://metalx.org/i386/Functions/Short"
	xmlns:int="http://metalx.org/i386/Functions/Integer"
	xmlns:hex="http://metalx.org/i386/Functions/Hexadecimal"
	xmlns:float="http://metalx.org/i386/Functions/Float"
	xmlns:dbl="http://metalx.org/i386/Functions/Double"
	xmlns:real="http://metalx.org/i386/Functions/Real"
	xmlns:lst="http://metalx.org/i386/Functions/List"
	xmlns:loop="http://metalx.org/i386/Functions/Loop"
	xmlns:con="http://metalx.org/i386/Functions/Console"
	xmlns:str="http://metalx.org/i386/Functions/String"
	xmlns:clsf="http://metalx.org/i386/Functions/Class"
	xmlns:var="http://metalx.org/Variable"
	xmlns:sys="http://metalx.org/i386/Functions/System"
	xmlns:cat="http://metalx.org/i386/Functions/Catalog"
	xmlns:obj="http://metalx.org/i386/Functions/Object"
	xmlns:asc="http://metalx.org/Ansi/Ascii"
	xmlns:fpu="http://metalx.org/Intel/80386/FpuOperators"
	xmlns:logic="http://metalx.org/Intel/80386/Logic"
	xmlns:math="http://metalx.org/Intel/80386/Math"
	xmlns:imm="http://metalx.org/Intel/80386/Immediate"
	xmlns:inc="http://metalx.org/Intel/80386/Increment"
	xmlns:cpu="http://metalx.org/Intel/80386/Operators"
	xmlns:op="http://metalx.org/Intel/80386/Operands"
	xmlns:cls="http://metalx.org/Class">

	<cls:field name="Port" type="http://metalx.org/Hexadecimal" offset="0"/>

	<cls:method name="Create" type="http://metalx.org/Ports/Parallel/ParallelPortWriter" static="true">
		<cls:parameter name="Port" type="http://metalx.org/Hexadecimal"/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullSIFromStack/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<mm:CheckOut length="4"/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddress/>

		<obj:Create length="4"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>
	
	<cls:method name="Skip" type="metalx.org/Ports/Parallel/ParallelPortWriter">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<cpu:PullSIFromStack/>
		
		<cpu:ExchangeOperandWithRegister/>
		<op:BP-SPRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:CX-SIAddress/>

		<cpu:Repeat/>
		<cpu:ReadFromDXPortToAL/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="SetPosition" type="http://metalx.org/Ports/ParallelPortWriter">
		<cls:parameter name="Position" type="http://metalx.org/Hexadecimal"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteObject" type="http://metalx.org/Ports/Parallel/ParallelPortWriter">
		<cls:parameter name="Value" type="http://metalx.org/Object"/>
		
		<cpu:PushDIToStack/>

		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<!--Get Value-->
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>
		
		<obj:GetDataLength/>
		
		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>

		<obj:GetData/>

		<!--Write Bytes-->
		<loop:For>
			<cpu:CopySIAddressToALAndIncrementSI/>
			
			<cpu:CallRelative/>
			<addressOf ref="writeByte" type="Relative32"/>
		</loop:For>

		<cpu:PullDIFromStack/>
		
		<cpu:ReturnToNearCaller/>
	</cls:method>
		
	<cls:method name="WriteBytes" type="http://metalx.org/List">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:CopyImmediateToDI/>
		<int>0</int>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteBoolean" type="http://metalx.org/Boolean">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:ReadFromDXPortToAL/>

		<bool:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteByte" type="http://metalx.org/Byte">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:ReadFromDXPortToAL/>
		<byte:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteShort" type="http://metalx.org/Short">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:Operand16/>
		<cpu:ReadFromDXPortToAX/>

		<short:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteInteger" type="http://metalx.org/Integer">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:ReadFromDXPortToAX/>

		<int:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteHexadecimal" type="http://metalx.org/Hexadecimal">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:ReadFromDXPortToAX/>

		<hex:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteFloat" type="http://metalx.org/Ports/Parallel/ParallelPortWriter">
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteDouble" type="http://metalx.org/Ports/Parallel/ParallelPortWriter">
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteReal" type="http://metalx.org/Ports/Parallel/ParallelPortWriter">
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="WriteString" type="http://metalx.org/Ports/Parallel/ParallelPortWriter">
		<cls:parameter name="Value" type="http://metalx.org/String"/>
		
		<cpu:PushDIToStack/>

		<!--Get Port-->
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<!--Get Value-->
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>
		
		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>
		
		<!--Write Characters-->
		<loop:For>
			<cpu:CopySIAddressToALAndIncrementSI/>
			
			<cpu:CallRelative/>
			<addressOf ref="writeByte" type="Relative32"/>
		</loop:For>

		<cpu:PullDIFromStack/>
		
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ToString" type="http://metalx.org/String">
		<cpu:CopyImmediateToDI/>
		<addressOf ref="className" type="Absolute32"/>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>

		<var:string id="className">Parallel Port Writer</var:string>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class" static="true">
		<cpu:CopyRegisterToOperand/>
		<op:BX-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<label id="writeByte">
		<!--Wait For Ready-->
		<cpu:IncrementDX/>
		
		<cpu:ExchangeBXWithAX/>

		<label id="waitForBusy"/>

		<cpu:ReadFromDXPortToAL/>

		<cpu:TestALWithImmediate/>
		<binary>00000001</binary>

		<!--<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="waitForBusy" type="Relative8"/>-->

		<!--Write Byte-->
		<cpu:DecrementDX/>
		
		<cpu:ExchangeBXWithAX/>

		<cpu:WriteALToDXPort/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToDXRegister/>
		<byte>2</byte>

		<cpu:ReadFromDXPortToAL/>

		<cpu:OrALWithImmediate/>
		<binary>00000001</binary>
	
		<cpu:WriteALToDXPort/>

		<cpu:AndALWithImmediate/>
		<binary>11111110</binary>
	
		<cpu:WriteALToDXPort/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:SubtractFromDXRegister/>
		<byte>2</byte>
		
		<cpu:ReturnToNearCaller/>
	</label>
</cls:class>